<?php

namespace App\Http\Controllers\Webdisk\Admin;

use App\Http\Controllers\Controller;
use App\Webdisk\MailSender;
use App\Webdisk\Model\ContributeInfo;
use Illuminate\Http\Request;

use function App\Webdisk\makeRes;

class ContributeController extends Controller
{
    function getStat(){
        $res['total'] = ContributeInfo::count();
        $res['pass'] = ContributeInfo::where(['status'=>'1'])->count();
        $res['refuse'] = ContributeInfo::where(['status'=>'-1'])->count();
        $res['wait'] = ContributeInfo::where(['status'=>0])->count();
        $last = ContributeInfo::select('updated_at')->orderBy('updated_at')->limit(1)->get();
        if(!$last->isEmpty()){
            $res['last'] = $last[0]['updated_at'];
        }
        return makeRes(1,$res);
    }

    function getVerifyList()
    {
        $primaryData = json_decode(ContributeInfo::where("status",0)->orderBy('id','desc')->paginate(10)->toJson(),true);
        $data = $primaryData['data'];
        $res = makeRes(1,$data);
        $res['total']= $primaryData['total'];
        return $res;
    }

    function getAllVerifyList()
    {
        $primaryData = json_decode(ContributeInfo::orderBy('id','desc')->paginate(10)->toJson(),true);
        $data = $primaryData['data'];
        $res = makeRes(1,$data);
        $res['total']= $primaryData['total'];
        return $res;
    }

    function refuse(Request $req)
    {
        $req->validate([
            'id'=>['required'],
            'message'=>['required']
        ]);
        $contributeInfo = ContributeInfo::find($_POST['id']);
        if($contributeInfo == null){
            return makeRes(-3,null,'无效的ID');
        }else if($contributeInfo->status != 0){
            return makeRes(-3,null,'不可重复处理');
        }

        try {
            unlink(env("TEMP_DIR").'/'.$contributeInfo->md5);
            $contributeInfo->status = -1;
            $contributeInfo->save();
            if( !(isset($_POST['noSendEmail']) && $_POST['noSendEmail'] == "true") ){
                $data = [
                    'primary_file'=>$contributeInfo->file,
                    'primary_path'=>$contributeInfo->path,
                    'msg'=>$_POST['message']
                ];
                $mailSender = new MailSender($contributeInfo->email,'咸鱼云 - 文件被拒收',view('refuseEmail',$data));
                $mailSender->send();
            }
        } catch (\Throwable $th) {
            return makeRes(-2,$contributeInfo,'服务器出错');
        }
        return makeRes();
    }

    function pass(Request $req)
    {
        $req->validate([
            'id'=>['required'],
            'path'=>['required'],
            'file'=>['required'],
            'message'=>['required']
        ]);
        $real_uri = $_POST['path'].'/'.$_POST['file'];
        $contributeInfo = ContributeInfo::find($_POST['id']);
        if($contributeInfo == null){
            return makeRes(-3,null,'无效的ID');
        }else if($contributeInfo->status != 0){
            return makeRes(-4,null,'已审核');
        }
        $old = env('TEMP_DIR')."/{$contributeInfo->md5}";
        $new = env('RES_ROOT')."/$real_uri";
        $contributeInfo->operater_id = $req->attributes->get("user")->id;
        $contributeInfo->save_path = $new;
        $contributeInfo->status = 1;
        try {
            if(!file_exists(env("RES_ROOT").'/'.$_POST['path'] )){
                mkdir(env("RES_ROOT").'/'.$_POST['path'],0775,true);
            }
        } catch (\Throwable $th) {
            return makeRes(-2,null,"权限不足 target:".env("RES_ROOT").'/'.$_POST['path']);
        }
        if( ! (isset($_POST['noSendEmail']) && $_POST['noSendEmail'] == "true") ){
            $data = [
                'primary_file'=>$contributeInfo->file,
                'primary_path'=>$contributeInfo->path,
                'save_path'=> $real_uri,
                'link'=>env('APP_URL').'/'.env('PUBLIC_PREFIX')."$real_uri",
                'msg'=>$_POST['message']
            ];
            $mailSender = new MailSender($contributeInfo->email,'咸鱼云 - 文件审核通过',view('passEmail',$data));
            $mailSender->send();
        }
        $contributeInfo->save();
        rename($old,$new);
        return makeRes();
    }
}
